import {
  myRequesy
} from '../../request/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 选项卡 
    tablist: [{
        name: "收到的评论",
        status: true,
        id: 0
      },
      {
        name: "发出的评论",
        status: false,
        id: 1
      }
    ],
    receivelist: [], // 收到的评论 列表
    setoutlist: [], // 发出的评论 列表
    RepageNum: 1, //收到评论的页码值
    GipageNum: 1, //发出的评论页码值
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getReceiveList()
  },

  // 顶部评论/切换的回调
  btnTab(e) {
    this.setData({
      RepageNum: 1,
      GipageNum: 1
    })
    let index = e.currentTarget.dataset.index
    let tablist = this.data.tablist
    tablist.map((item, i) => i === index ? item.status = true : item.status = false)
    this.setData({
      tablist
    })
    if (index == 1) {
      this.getGiveList()
    } else {
      this.getReceiveList()
    }
  },

  // 获取用户收到评论列表
  getReceiveList() {
    let that = this
    myRequesy({
      url: 'user/received-comments',
      data: {
        page: that.data.RepageNum
      }
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          receivelist: res.data.data,
          RenextPage: res.data.next_url
        })
      }
      console.log(res);

    })
  },


  // 获取用户发出的评论列表
  getGiveList() {
    let that = this
    myRequesy({
      url: 'user/send-comments',
      data: {
        page: that.data.GipageNum
      }
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          setoutlist: res.data.data,
          GinextPage: res.data.next_url
        })
      }
    })
  },



  // 上拉加载更多收到评论
  getBomRecei() {
    let that = this
    if (that.data.RenextPage === null) {
      return
    } else {
      let that = this
      that.data.RepageNum += 1
      myRequesy({
        url: 'user/received-comments',
        data: {
          page: this.data.RepageNum
        }
      }).then(res => {
        if (res.data.code === 0) {
          that.setData({
            receivelist: [...that.data.receivelist, ...res.data.data],
            RenextPage: res.data.next_url
          })
        }
      })
    }
  },

  // 上拉加载更多发出评论
  getBomGive() {
    let that = this
    if (that.data.GinextPage === null) {
      return
    } else {
      that.data.GipageNum += 1
      myRequesy({
        url: 'user/send-comments',
        data: {
          page: that.data.GipageNum
        }
      }).then(res => {
        console.log(res);
        if (res.data.code === 0) {
          that.setData({
            setoutlist: [...that.data.setoutlist, ...res.data.data],
            GinextPage: res.data.next_url
          })
        }
      })
    }
  },


  //点击头像跳转到用户动态
  headleNav(e) {
    wx.navigateTo({
      url: '/pages/dynamic/dynamic?id=' + e.currentTarget.dataset.id,
    })
  },

  // 点击评论图片跳转到
  headleDeta(e) {
    console.log(e);
    wx.navigateTo({
      url: '/pages/details/details?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getBomRecei()
    this.getBomGive()




  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})